package com.cskaoyan.util;

import com.cskaoyan.exception.MobileValidationException;
import com.cskaoyan.exception.NameValidationException;
import com.cskaoyan.exception.PwdValidationException;
import com.cskaoyan.exception.QQValidationException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

/**
 * @author lxs
 * @date 2022/05/07 11:40
 */
public class ValidationUtil {

    public static void validInfo(BindingResult bindingResult) throws NameValidationException, PwdValidationException {
        if (bindingResult.hasErrors()) {
            String defaultMessage;
            // 拿到成员变量的错误
            FieldError fieldError = bindingResult.getFieldError();
            String field = fieldError.getField();
            if ("username".equals(field)) {
                defaultMessage = fieldError.getDefaultMessage();
                throw new NameValidationException(defaultMessage);
            }
            if("password".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                throw new PwdValidationException(defaultMessage);
            }
        }
    }


    /**
     * @author: zhoukang
     * @description: TODO 手机号码校验
     * @date: 2022/5/7 21:00
     */
    public static void validMobile(BindingResult bindingResult) throws MobileValidationException {
        if (bindingResult.hasErrors()){
            String defaultMessage;
            //获取成员变量错误
            FieldError fieldError = bindingResult.getFieldError();
            //获取错误的参数名称
            String field = fieldError.getField();
            if ("mobile".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
        }
    }

    /**
     * @author: huzhouming
     * @description: 配置管理，商场配置格式异常报错
     * @date: 2022/5/10
     * @param null
     * @return
     */
    public static void validMall(BindingResult bindingResult) throws MobileValidationException, QQValidationException {
        if (bindingResult.hasErrors()){
            String defaultMessage;
            //获取成员变量错误
            FieldError fieldError = bindingResult.getFieldError();
            //获取错误的参数名称
            String field = fieldError.getField();
            if("market_mall_longitude".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
            if("market_mall_latitude".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
            if ("market_mall_address".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
            if ("market_mall_phone".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
            if("market_mall_qq".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new QQValidationException(defaultMessage);
            }
        }
    }

    /**
     * @author: huzhouming
     * @description: TODO 运费配置输入格式控制
     * @date: 2022/5/10 
     * @param null
     * @return 
     */
    public static void validExpress(BindingResult bindingResult) throws MobileValidationException {
        if (bindingResult.hasErrors()){
            String defaultMessage;
            //获取成员变量错误
            FieldError fieldError = bindingResult.getFieldError();
            //获取错误的参数名称
            String field = fieldError.getField();
            if ("market_express_freight_min".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
            if ("market_express_freight_value".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
        }
    }

    /**
     * @author: huzhouming
     * @description: TODO 订单配置输入格式控制
     * @date: 2022/5/10
     * @param null
     * @return
     */
    public static void validOrder(BindingResult bindingResult) throws MobileValidationException {
        if (bindingResult.hasErrors()){
            String defaultMessage;
            //获取成员变量错误
            FieldError fieldError = bindingResult.getFieldError();
            //获取错误的参数名称
            String field = fieldError.getField();
            if ("market_order_unconfirm".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
            if ("market_order_unpaid".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
            if ("market_order_comment".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
        }
    }

    /**
     * @author: huzhouming
     * @description: 微信小程序输入格式控制
     * @date: 2022/5/10
     * @param null
     * @return
     */
    public static void validWx(BindingResult bindingResult) throws MobileValidationException {
        if (bindingResult.hasErrors()){
            String defaultMessage;
            //获取成员变量错误
            FieldError fieldError = bindingResult.getFieldError();
            //获取错误的参数名称
            String field = fieldError.getField();
            if ("market_wx_index_new".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
            if ("market_wx_index_topic".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
            if ("market_wx_share".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
            if ("market_wx_index_brand".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
            if ("market_wx_catlog_goods".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
            if ("market_wx_catlog_list".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
            if ("market_wx_index_hot".equals(field)){
                defaultMessage = fieldError.getDefaultMessage();
                //抛出异常
                throw new MobileValidationException(defaultMessage);
            }
        }
    }
}
